package com.sport.facilities.booking.system.ui;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.sport.facilities.booking.system.R;
import com.sport.facilities.booking.system.model.Booking;

public class BookingModelListAdaptor extends BaseAdapter {
	private Context context;
	private List<Booking> bookingList;
	private int selectedPos = -1;

	public BookingModelListAdaptor(Context context, List<Booking> bookingList) {
		this.context = context;
		this.bookingList = bookingList;
	}

	public void setBookingList(List<Booking> booklist) {
		this.bookingList = booklist;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		LayoutInflater mInflater = (LayoutInflater) context
				.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.make_booking_list_item,
					null);
			holder = new ViewHolder();
			holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
			holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
			holder.txtBody = (TextView) convertView.findViewById(R.id.body);
			holder.txtRemark = (TextView) convertView.findViewById(R.id.remark);
			fnSetSelectedColor(convertView, position);
			convertView.setTag(holder);
		} else {
			fnSetSelectedColor(convertView, position);
			holder = (ViewHolder) convertView.getTag();
		}

		Booking b = (Booking) getItem(position);

		holder.txtTitle.setText("Centre : " + b.getCentre());
		holder.txtBody.setText("Court : " + b.getCourt() + "\nFrom : "
				+ b.getDate() + "\nTo : " + b.getTime());
		holder.txtRemark.setText(b.getPrice());
		holder.imageView.setImageResource(fnGetImageFromActivity(b
				.getActivity()));
		return convertView;
	}

	private void fnSetSelectedColor(View convertView, int position) {
		if (selectedPos == position) {
			convertView.setBackgroundColor(Color.CYAN);
		} else {
			convertView.setBackgroundColor(Color.WHITE);
		}
	}

	public long getItemId(int position) {
		return position;
	}

	public Object getItem(int position) {
		return bookingList.get(position);
	}

	public int getCount() {
		return bookingList.size();
	}

	/* private view holder class */
	private class ViewHolder {
		ImageView imageView;
		TextView txtTitle;
		TextView txtBody;
		TextView txtRemark;
	}

	private int fnGetImageFromActivity(String activity) {
		int imageLocation = R.drawable.information;
		if (activity.equalsIgnoreCase("Activities Room")) {
		}

		if (activity.equalsIgnoreCase("Badminton")) {
			imageLocation = R.drawable.badminton;
		}
		if (activity.equalsIgnoreCase("Fitness Training")) {
			imageLocation = R.drawable.fitness;
		}
		if (activity.equalsIgnoreCase("Hall Fitness Room")) {
			imageLocation = R.drawable.fitness;
		}
		if (activity.equalsIgnoreCase("Indoor Basketball")) {
			 imageLocation = R.drawable.basketball;
		}
		if (activity.equalsIgnoreCase("Indoor Volleyball")) {
			 imageLocation = R.drawable.volleyball;
		}
		if (activity.equalsIgnoreCase("JSC 5 a-side Soccer")) {
			imageLocation = R.drawable.soccer;
		}
		if (activity.equalsIgnoreCase("JSC Basketball")) {
			 imageLocation = R.drawable.basketball;
		}
		if (activity.equalsIgnoreCase("JSC Golf")) {
			 imageLocation = R.drawable.golf;
		}
		if (activity.equalsIgnoreCase("JSC Handball")) {
			 imageLocation = R.drawable.handball;
		}
		if (activity.equalsIgnoreCase("JSC Soccer")) {
			imageLocation = R.drawable.soccer;
		}
		if (activity.equalsIgnoreCase("JSC Tennis")) {
			 imageLocation = R.drawable.tennis;
		}
		if (activity.equalsIgnoreCase("JSC Volleyball")) {
			 imageLocation = R.drawable.volleyball;
		}
		if (activity.equalsIgnoreCase("Shaw Golf")) {
			 imageLocation = R.drawable.golf;
		}
		if (activity.equalsIgnoreCase("Soccer")) {
			imageLocation = R.drawable.soccer;
		}
		if (activity.equalsIgnoreCase("Squash")) {
			imageLocation = R.drawable.squash;
		}
		if (activity.equalsIgnoreCase("Tennis")) {
			 imageLocation = R.drawable.tennis;
		}
		if (activity.equalsIgnoreCase("Table tennis")) {
			 imageLocation = R.drawable.tabletennis;
		}

		return imageLocation;
	}

	public Booking getSelectedBooking() {
		if (selectedPos >= 0) {
			Booking b = (Booking) getItem(selectedPos);
			return b;
		}
		return null;
	}

	public int getSelectedIndex() {
		return selectedPos;
	}

	public void setSelectedPosition(int pos) {
		selectedPos = pos;
		// inform the view of this change
		notifyDataSetChanged();
	}
}